{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch:1000 cost:0.000019\n",
      "epoch:2000 cost:0.000004\n",
      "epoch:3000 cost:0.000002\n",
      "epoch:4000 cost:0.000001\n",
      "epoch:5000 cost:0.000000\n",
      "sorry hate you is Bad Mean...\n"
     ]
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "sentences = [\"i love you\",\"he loves me\", \"she likes baseball\", \"i hate you\",\"sorry for that\", \"this is awful\"]\n",
    "labels = [1,1,1,0,0,0]\n",
    "#做的是情感分类\n",
    "wordlist = ' '.join(sentences).split()\n",
    "wordlist = list(set(wordlist))\n",
    "word_num = {w:i for i,w in enumerate(wordlist)}\n",
    "vocab_size = len(word_num)\n",
    "\n",
    "num_class = 2# 0表示好 1表示坏\n",
    "sentence_size = 3#句子长度\n",
    "embedding_size = 2\n",
    "filter_sizes = [2,2,2]#卷积核的size\n",
    "num_filter = 3#卷积核个数\n",
    "\n",
    "input_x = []\n",
    "for sen in sentences:\n",
    "    input_x.append(np.asarray([word_num[word] for word in sen.split()]))\n",
    "output = []\n",
    "for label in labels:\n",
    "    output.append(np.eye(num_class)[label])\n",
    "\n",
    "X = tf.placeholder(tf.int32,[None,sentence_size])#batch_size sentences_size\n",
    "Y = tf.placeholder(tf.int32,[None,num_class])#batch_size num_class\n",
    "\n",
    "W = tf.Variable(tf.random_uniform([vocab_size,embedding_size],-1.0,1.0))\n",
    "embed = tf.nn.embedding_lookup(W,X)#batch_size sentences_size embedding_size\n",
    "embed = tf.expand_dims(embed,-1)#batch_size sentences_size embedding_size -1\n",
    "\n",
    "pool_output = []\n",
    "for i,filter_size in enumerate(filter_sizes):\n",
    "    filter_shape = [filter_size,embedding_size,1,num_filter]#embed batch_size sentences_size embedding_size -1\n",
    "    W = tf.Variable(tf.truncated_normal(filter_shape,stddev=0.1))\n",
    "    b = tf.constant(0.1,shape=[num_filter])\n",
    "    \n",
    "    conv = tf.nn.conv2d(embed,#卷积\n",
    "                        W,\n",
    "                        strides = [1,1,1,1],\n",
    "                        padding = 'VALID')\n",
    "    h = tf.nn.relu(tf.nn.bias_add(conv,b))#非线性激活\n",
    "    #max_pooling\n",
    "    pool = tf.nn.max_pool(h,\n",
    "                        ksize = [1,sentence_size-filter_size+1,1,1],# [batch_size, filter_height, filter_width, channel]\n",
    "                        strides = [1,1,1,1],\n",
    "                        padding = 'VALID')\n",
    "    pool_output.append(pool)\n",
    "\n",
    "filter_total = len(filter_sizes)*num_filter#卷积核个数*通道数\n",
    "h_pool = tf.concat(pool_output,num_filter)# h_pool : [batch_size(=6), output_height(=1), output_width(=1), channel(=1) * 3]\n",
    "\n",
    "h_pool = tf.reshape(h_pool,shape=[-1,filter_total])\n",
    "\n",
    "Weights = tf.get_variable('W',shape=[filter_total,num_class],initializer=tf.contrib.layers.xavier_initializer())\n",
    "bias = tf.Variable(tf.constant(0.1, shape=[num_class]))\n",
    "model = tf.nn.xw_plus_b(h_pool,Weights,bias)\n",
    "\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y,logits=model))\n",
    "optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)\n",
    "\n",
    "prediction = tf.nn.softmax(model)\n",
    "prediction = tf.argmax(prediction,1)\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for i in range(5000):\n",
    "        _,loss = sess.run([optimizer,cost],feed_dict = {X:input_x,Y:output})\n",
    "        if (i+1) % 1000 == 0:\n",
    "            print('epoch:%d cost:%.6f' % ((i+1),loss))\n",
    "            \n",
    "    #test\n",
    "    text = 'sorry hate you'\n",
    "    test_num = [[word_num[w] for w in text.split()]]\n",
    "    predict = sess.run([prediction],feed_dict={X:test_num})\n",
    "    if predict[0][0] == 0:\n",
    "        print(text,\"is Bad Mean...\")\n",
    "    else:\n",
    "        print(text,\"is Good Mean!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
